﻿Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml
Imports System.Text.RegularExpressions

Public Class searchresult
    Public query
    Dim filename As String
    Private Sub searchresult_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text += query
        Dim req As HttpWebRequest = WebRequest.Create("http://search.1g1g.com/public/songs?encoding=utf8&query=" + query)
        Dim res As HttpWebResponse = req.GetResponse()
        Dim strm As StreamReader = New StreamReader(res.GetResponseStream(), Encoding.UTF8)
        Dim sline As String
        sline = strm.ReadToEnd()

        filename = My.Computer.FileSystem.GetTempFileName()
        My.Computer.FileSystem.WriteAllText(filename, sline, True)

        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load(filename)
        Dim xmlNode As System.Xml.XmlNode = xmlDoc.SelectSingleNode("//" + "songlist")

        For i = 0 To xmlNode.ChildNodes.Count - 1
            Dim xmlNodes As System.Xml.XmlNode = xmlNode.ChildNodes(i).ChildNodes(0)
            Dim id = xmlNodes.InnerText.ToString

            xmlNodes = xmlNode.ChildNodes(i).ChildNodes(1)
            Dim name = xmlNodes.InnerText.ToString

            xmlNodes = xmlNode.ChildNodes(i).ChildNodes(2)
            Dim singer = xmlNodes.InnerText.ToString

            xmlNodes = xmlNode.ChildNodes(i).ChildNodes(3)
            Dim album = xmlNodes.InnerText.ToString

            xmlNodes = xmlNode.ChildNodes(i).ChildNodes(4)
            Dim user = xmlNodes.InnerText.ToString

            Dim item As New ListViewItem(id)
            item.SubItems.Add(name)
            item.SubItems.Add(singer)
            item.SubItems.Add(album)
            item.SubItems.Add(user)

            ListView1.Items.Add(item)
        Next

        If ListView1.Items.Count = 0 Then
            MsgBox("对不起，系统无法搜索您提供的关键字，原因是：" & vbCrLf & "系统没有此关键字的歌曲，请检查关键字。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
            Me.Close()
        Else
            Me.Text += " - 共有 " & ListView1.Items.Count.ToString & " 项"
        End If
    End Sub
    Dim singid, singname
    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        If ListView1.SelectedItems.Count > 0 Then
            singid = ListView1.SelectedItems(0).Text
            singname = ListView1.SelectedItems(0).SubItems(1).Text & " - " & ListView1.SelectedItems(0).SubItems(2).Text
        End If
    End Sub

    Private Sub 分享链接ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 分享链接ToolStripMenuItem.Click
        My.Computer.Clipboard.SetText("http://www.1g1g.com/#" & singid)
        MsgBox("分享链接已复制到剪贴板！")
    End Sub

    Private Sub 歌曲IDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 歌曲IDToolStripMenuItem.Click
        My.Computer.Clipboard.SetText(singid)
        MsgBox("ID已复制到剪贴板！")
    End Sub

    Private Sub 播放选中歌曲ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 播放选中歌曲ToolStripMenuItem.Click
        Form1.sendCommand("command", "play", singname)
    End Sub
End Class